package com.tianci.appstore;

import android.content.pm.PackageManager;
import android.os.Bundle;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.dataprovider.AppDataProvider;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.mainpage.SkyMyAppPage;
import com.tianci.appstore.ui.widget.SkyAppPager;

import java.util.ArrayList;
import java.util.List;

public class ChildrenActivity extends SkyBaseActivity {
    SkyMyAppPage contentView;
    private List<SkyAppBaseData> appList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        LogUtils.d("child", "-------------will load data");
        appList = null;
        loadChildrenApps();

        contentView = new SkyMyAppPage(this, SkyAppPager.Direction.HORIZONTAL, true);
        if (this.appList != null) {
            contentView.getController().refreshContent(appList);
        }
        SkinManager.setBackground(contentView, R.drawable.child_myapp_bg);
        setContentView(contentView);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
    }

    private void loadChildrenApps() {
        new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                List<SkyListAppData> dataList = AppDataProvider.getInstance().getLocalData().getChildApps();
                List<SkyAppBaseData> childrenList = new ArrayList<SkyAppBaseData>();
                for (SkyListAppData data : dataList) {
                    try {
                        ChildrenActivity.this.getPackageManager().getPackageInfo(
                                data.getPkgName(), PackageManager.GET_CONFIGURATIONS);
                        childrenList.add(data);
                    } catch (Exception e) {
                    }
                }
                onChildrenListLoaded(childrenList);
                super.run();
            }
        }.start();
    }

    private void onChildrenListLoaded(final List<SkyAppBaseData> appList) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                LogUtils.d("child", "555555555555555555");
                if (contentView != null) {
                    LogUtils.d("child", "6666666666666666666");
                    contentView.getController().refreshContent(appList);
                    contentView.invalidate();
                } else {
                    LogUtils.d("child", "77777777777777777777");
                    ChildrenActivity.this.appList = appList;
                }
            }
        });
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "应用圈儿童应用";
    }
}
